package string;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/6/13
 * Time: 19:37
 */

public class StringBuilder_StringBuffer_Demo {
    public static void main1(String[] args) {
        String string = "Hello";
        string = string + "World";
        System.out.println(string);//输出Hello World
    }

    public static void main(String[] args) {
        //String
        long start = System.currentTimeMillis();
        String string = "";
        for (int i = 0; i < 10000; i++) {
            string = string + i;
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);//输出结果在60左右
        //StringBuilder
        start = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder("");
        for (int i = 0; i < 10000; i++) {
            stringBuilder.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);//输出结果不超过2
    }
}
